package cn.clwxy.gdz.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.clwxy.gdz.bo.NewOrderByTeacher;
import cn.clwxy.gdz.bo.TeacherInfoPageOrderListBean;
import cn.clwxy.gdz.dao.OrderDao;
import cn.clwxy.gdz.dao.OrderDaoImpl;
import cn.clwxy.gdz.entity.Teacher;
import cn.clwxy.gdz.service.LoginService;
import cn.clwxy.gdz.service.LoginServiceImpl;
import cn.clwxy.gdz.service.NewOrderByTeacherService;
import cn.clwxy.gdz.service.NewOrderByTeacherServiceImpl;
import cn.clwxy.gdz.service.TeacherInfoPageOrderListBeanDao;
import cn.clwxy.gdz.service.TeacherInfoPageOrderListBeanDaoImpl;
import cn.clwxy.gdz.util.JsonSerializeHelper;
import cn.clwxy.gdz.util.MD5Encrypter;

public class NewOrderByTeacherServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		String login_phone=request.getParameter("login_phone");
		Integer studentId=Integer.valueOf(request.getParameter("studentId"));
		Integer teacherId=Integer.valueOf(request.getParameter("teacherId"));
		String subjects[]=request.getParameterValues("subjects");
		String gradeName=request.getParameter("gradeName");
		String login_pwd=request.getParameter("login_pwd");
		
		
		OrderDao orderDao=new OrderDaoImpl();
		System.out.println(studentId);
		System.out.println(teacherId);
		int hasClick=orderDao.findOrderBySIdTIdInitiator(studentId, teacherId, 2);
		System.out.println(hasClick);
		if(hasClick>0){
			System.out.println("今天已经预约过啦！客服正在帮您安排！");
			response.getWriter().write("-1");
			return;
			
		}
		
		int subjectLength=subjects.length;
		Integer subjectIds[]=new Integer[subjectLength];
		int i=0;
		for (String subjectName : subjects) {
			subjectIds[i]=Integer.valueOf(subjectName);
			i++;
		}
		
//		MD5��������м���
		login_pwd=MD5Encrypter.encrypt(login_phone+login_pwd+login_phone);
		NewOrderByTeacher newOrderByTeacher=new NewOrderByTeacher();
//判断手机号密码是否正确
		LoginService loginService=new LoginServiceImpl();
		Teacher teacher=loginService.getTeaher(login_phone, login_pwd);
		if(null==teacher){
			System.out.println("老师的手机号或者密码错误");
			response.getWriter().write("0");
			return;
		}

		newOrderByTeacher.setGradeName(gradeName);
		newOrderByTeacher.setStudentId(studentId);
		newOrderByTeacher.setSubjectIds(subjectIds);
		newOrderByTeacher.setTeacherId(teacherId);
		newOrderByTeacher.setTeacherPhone(login_phone);
		newOrderByTeacher.setTeacherPwd(login_pwd);
		NewOrderByTeacherService newOrderByTeacherService=new NewOrderByTeacherServiceImpl();
		Integer orderId=newOrderByTeacherService.addOrderByTeacher(newOrderByTeacher);
		System.out.println(orderId);
		if(orderId>0){
			response.getWriter().write(String.valueOf(orderId));
		}else{
			response.getWriter().write("0");
		}
	}
}
